package pers.wq.controller.user;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import pers.wq.controller.BaseIndexAction;
import pers.wq.controller.PublicListAction;
import pers.wq.model.StudentModel;
import pers.wq.model.UserModel;
import pers.wq.model.bean.BaseBean;
import pers.wq.model.bean.Major;
import pers.wq.model.bean.MajorRequest;
import pers.wq.model.bean.SelectMajor;
import pers.wq.model.bean.User;
import pers.wq.util.TagManager;

public class IndexAction extends PublicListAction{
	public static String USER_LOGIN_TAG = "USER_LOGIN";
	private User user;
	private UserModel userModel;
	private Major major;
	private MajorRequest majorRequest;
	private SelectMajor selectMajor;
	private BaseBean[] objs ;
	public IndexAction() {
		// TODO Auto-generated constructor stub
		super("user");
		setXmlPath("thList4user.xml");
		userModel = new UserModel();
		setLoginTag(USER_LOGIN_TAG);
		System.out.println("constructor");
	}
	public String index() {
		return SUCCESS;
	}
	public String login() {
		// TODO Auto-generated method stub
		String reStr = ERROR;
		user = userModel.checkLogin(user);
		if(null != user) {
			System.out.println("User IndexAction " + user.getCollege().getCid());
			this.loginSession(user.getCollege().getCid());
			reStr = SUCCESS;
		}
		return reStr;
	}
	@Override
	public String formTag() {
		// TODO Auto-generated method stub
		System.out.println("user IndexAction forTag");
		return TagManager.formTag(getDataListId(),"user",getMethod());
	}
	@Override
	public Map<String, String> mapTag() {
		// TODO Auto-generated method stub
		System.out.println("user IndexAction mapTag");
		return _mapTag(objs);
	}
	@Override
	public String add() throws Exception {
		// TODO Auto-generated method stub
		BaseBean[] objs = {major,majorRequest,selectMajor};
		String reStr = this._add(objs);
		return reStr == SUCCESS ? CHAIN : ERROR;
	}
	@Override
	public String se() throws Exception {
		// TODO Auto-generated method stub
		objs = new BaseBean[]{major,majorRequest,selectMajor};
		String reStr = _se(objs)  == EA ? super.se() : ERROR;
		return reStr;
	}
	@Override
	public String sa() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("User IndexAction sa");
		objs = new BaseBean[]{major,majorRequest,selectMajor};
		return super.sa();
	}
	@Override
	public String update() throws Exception {
		// TODO Auto-generated method stub
		BaseBean[] objs = {major,majorRequest,selectMajor};
		if(null != objs[2] && ((SelectMajor)objs[2]).getState()!=0) {
			String mid = ((SelectMajor)objs[2]).getMajor().getMid();
			String sid = ((SelectMajor)objs[2]).getStudent().getSid() + "";
			StudentModel.uploadMid(sid, mid);
		}
		String reStr = this._update(objs);
		return reStr == SUCCESS ? CHAIN : ERROR;
	}
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		BaseBean[] objs = {major,majorRequest,selectMajor};
		System.out.println("delete in");
		try{
			if(this._delete(objs).equals(SUCCESS)){
				this.setJsonMapSuccess("Delete success!");
			}else{
				this.setJsonMapError("Delete error!");
			}
		}catch(Exception e){
			this.setJsonMapError("Delete error");
		}
		System.out.println(jsonMap);
		return JSON;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Major getMajor() {
		return major;
	}
	public void setMajor(Major major) {
		this.major = major;
	}
	public MajorRequest getMajorRequest() {
		return majorRequest;
	}
	public void setMajorRequest(MajorRequest majorRequest) {
		this.majorRequest = majorRequest;
	}
	public SelectMajor getSelectMajor() {
		return selectMajor;
	}
	public void setSelectMajor(SelectMajor selectMajor) {
		this.selectMajor = selectMajor;
	}
}
